/**
 * InitialisationPanel.java
 * 
 * Panel used to present options to the user for initialising the world. 
 * 
 */
package com.swin.energy;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class InitialisationPanel extends JPanel implements ActionListener {

	private JFrame frameParent;
	private MainFrame frameMainParent;

	private static final long serialVersionUID = -4217914697396136881L;

	public static int MAX_REGIONS = 10;
	public static int MIN_HOUSES = 20;
	public static int MAX_HOUSES = 50;
	public static int MIN_APPS = 3;
	public static int MAX_APPS = 5;

	private JComboBox<Integer> numberOfRegions;
	private JComboBox<Integer> numberOfHouses;
	private JComboBox<Integer> numberOfApps;

	DefaultComboBoxModel<Integer> model;
	private JButton addButton = new JButton("Create World");

	private JLabel nameLabel = new JLabel("World Name");
	private JLabel regionLabel = new JLabel("Number of Regions");
	private JLabel houseLabel = new JLabel("Max. num of houses/region");
	private JLabel appsLabel = new JLabel("Max. num of apps/region");

	private JTextField nameOfWorldText = new JTextField(20);

	public InitialisationPanel(JFrame frame, MainFrame mainFrame) {
		frameParent = frame;
		frameMainParent = mainFrame;

		// Set regions combobox
		Integer numberOfReasons[] = new Integer[MAX_REGIONS];

		for (int i = 0; i < MAX_REGIONS; i++)
			numberOfReasons[i] = i + 1;

		numberOfRegions = new JComboBox<Integer>(numberOfReasons);
		addButton.addActionListener(this);

		int size = MAX_HOUSES - MIN_HOUSES + 1;
		Integer numberOfHousesPerRegion[] = new Integer[size];

		for (int i = 0; i < (size); i++)
			numberOfHousesPerRegion[i] = MIN_HOUSES + i;

		numberOfHouses = new JComboBox<Integer>(numberOfHousesPerRegion);

		// Set house combobox
		size = MAX_APPS - MIN_APPS + 1;
		Integer numberOfAppsPerHouse[] = new Integer[size];

		for (int i = 0; i < (size); i++)
			numberOfAppsPerHouse[i] = MIN_APPS + i;

		numberOfApps = new JComboBox<Integer>(numberOfAppsPerHouse);

		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		JPanel panel3 = new JPanel();
		JPanel panel4 = new JPanel();
		JPanel panel5 = new JPanel();

		// set the layout in Java
		this.setLayout(new GridLayout(5, 1));
		panel1.setLayout(new FlowLayout());
		panel2.setLayout(new FlowLayout());
		panel3.setLayout(new FlowLayout());
		panel4.setLayout(new FlowLayout());
		panel5.setLayout(new FlowLayout());

		panel1.add(nameLabel);
		panel1.add(nameOfWorldText);

		panel2.add(regionLabel);
		panel2.add(numberOfRegions);

		panel3.add(houseLabel);
		panel3.add(numberOfHouses);

		panel4.add(appsLabel);
		panel4.add(numberOfApps);

		panel5.add(addButton);

		this.add(panel1);
		this.add(panel2);
		this.add(panel3);
		this.add(panel4);
		this.add(panel5);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		Object source = e.getSource();

		if (source == addButton) {

			String regions = numberOfRegions.getSelectedItem().toString();
			String houses = numberOfHouses.getSelectedItem().toString();
			String appsPerHouse = numberOfApps.getSelectedItem().toString();

			frameParent.dispose();
			Initialisation init = new Initialisation();
			init.setNosOfRegions(Integer.parseInt(regions));
			init.setMaxNumberOfHouses(Integer.parseInt(houses));
			init.setNumberOfAppliances(Integer.parseInt(appsPerHouse));
			init.setName(nameOfWorldText.getText());
			frameMainParent.setupInitialisationData(init);
			frameMainParent.showPanels();
		}
	}

}
